package com.readbook.action;


import com.alibaba.fastjson.JSONArray;
import com.readbook.service.QuestionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/question")
public class question {
    @Resource
    private QuestionService questionService;


    /**
     * @author 牟明艳
     * @comments 根据书籍id查询是否上传题目
     * @time 2019年9月29日12:20:35
     */
    @RequestMapping("getQuestionBybid.html")
    @ResponseBody
    public List<Map<String, Object>> getQuestionBybid(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("bid", bid);
        List<Map<String, Object>> list1 = questionService.getQuestionBybid(gmap);
        return list1;
    }


    /**
     * @author 牟明艳
     * @comments 根据书籍id查询用户是否已经答题
     * @time 2019年9月29日12:20:35
     */
    @RequestMapping("getMyAnswerBybid.html")
    @ResponseBody
    public List<Map<String, Object>> getMyAnswerBybid(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        String usid = request.getParameter("usid");
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("bid", bid);
        gmap.put("usid", usid);
        List<Map<String, Object>> list1 = questionService.getMyAnswerBybid(gmap);
        return list1;
    }


    /**
     * @author 牟明艳
     * @comments 保存答案
     * @time 2019年9月19日21:22:43
     */
    @RequestMapping("insertBookanswer.html")
    @ResponseBody
    public Map<String, Object> insertBookanswer(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> smap = new HashMap<String, Object>();
        String da = request.getParameter("da");
        Map<String, Object> remap = new HashMap<String, Object>();
        String usid = request.getParameter("usid");
        String username = request.getParameter("username");

        List<Map<String, Object>> listr = (List<Map<String, Object>>) JSONArray.parse(da);
        for (int i = 0; i < listr.size(); i++) {
            Map<String, Object> getmap = listr.get(i);
            String aid = UUID.randomUUID().toString().replaceAll("-", "");
            getmap.put("aid", aid);
            getmap.put("usid", usid);
            getmap.put("username", username);
            int k = questionService.insertBookanswer(getmap);
            if (k <= 0) {
                remap.put("msg", "保存失败!");
                remap.put("result", false);
                return remap;
            }
            //如果答案和正确答案相符合 则加积分1
            String bh = getmap.get("bh").toString();
            String answer = getmap.get("answer").toString();
            String zqda = getmap.get("zqda").toString();
            if (bh.equals(zqda) || answer.equals(zqda)) {
                //记录至积分记录文件
                Map<String, Object> jfmap = new HashMap<String, Object>();
                String jfid = UUID.randomUUID().toString().replaceAll("-", "");
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                String addtime = df.format(new Date());
                jfmap.put("jfid", jfid);
                jfmap.put("lyid", aid);
                jfmap.put("jfly", "bookanswer");//积分来源 打卡
                jfmap.put("addtime", addtime);
                jfmap.put("usid", usid);
                jfmap.put("username", username);
                jfmap.put("jfnum", "1");
                questionService.savejfjl(jfmap);
            }


        }
        //获取答对题的数目
        remap.put("msg", "保存成功!");
        remap.put("result", true);
        return remap;
    }


    /**
     * @author 牟明艳
     * @comments 获取我的诗词大会情况
     * @time 2019年9月20日14:23:26
     */
    @RequestMapping("getUserDtqk.html")
    @ResponseBody
    public Map<String, Object> getUserDtqk(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("bid", bid);
        String usid = request.getParameter("usid");
        gmap.put("usid", usid);
        List<Map<String, Object>> list1 = questionService.getUserDtqk(gmap);
        Map<String, Object> rmap = new HashMap<String, Object>();
        if (list1.size() > 0) {
            rmap = list1.get(0);
            String zql = rmap.get("ZQL").toString();
            int c = Integer.parseInt(zql) * 60;
            int m = 100;
            double n = (double) c / m;
            int zql1 = (int) Math.ceil(n);
            rmap.put("zql1", zql1);
        }
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments 获取答题积分排名
     * @time 2019年9月20日14:22:52
     */
    @RequestMapping("getAnswerpmBynj.html")
    @ResponseBody
    public Map<String, Object> getAnswerpmBynj(HttpServletRequest request, HttpServletResponse response) {
        String bid = request.getParameter("bid");
        Map<String, Object> gmap = new HashMap<String, Object>();
        gmap.put("bid", bid);
        String usid = request.getParameter("usid");
        gmap.put("usid", usid);

        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int startn = page * limit - limit;//起始
        int endn = page * limit;//结束
        gmap.put("start", startn);
        gmap.put("end", endn);

        List<Map<String, Object>> list1 = questionService.getAnswerpmBynj(gmap);
        int totalnum = questionService.countAnswerpmBynj(gmap);

        double c = (double) totalnum / limit;
        int totalpage = (int) Math.ceil(c);

        Map<String, Object> rmap = new HashMap<String, Object>();
        rmap.put("list", list1);
        rmap.put("totalpage", totalpage);
        rmap.put("totalnum", totalnum);
        return rmap;
    }


}
